package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.common.util.DateUtils;
import org.footballmanager.pandafootball.system.domain.FootTax;
import org.footballmanager.pandafootball.system.form.impl.FootTaxSearchForm;
import org.footballmanager.pandafootball.system.service.FootTaxService;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Slf4j
@Service("footTaxService")
public class FootTaxServiceImpl extends ServiceImpl<FootTax> implements FootTaxService {
    /**
     * 获取交易税
     *
     * @return 百分比
     */
    @Override
    public double getDealTax() {
        FootTax tax = this.get(1L);
        if (tax != null) {
            return tax.getFsd();
        }
        return 0D;
    }

    /**
     * 获取解约返还百分比
     *
     * @return 百分比
     */
    @Override
    public double getDismissTax() {
        FootTax tax = this.get(2L);
        if (tax != null) {
            return tax.getFsd();
        }
        return 0D;
    }

    /**
     * 查询
     *
     * @param entityname        实体类名
     * @param map               参数
     * @param footTaxSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootTaxSearchForm footTaxSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footTaxSearchForm);
        Page<FootTax> page = PageHelper.startPage(footTaxSearchForm.getPageIndex(), footTaxSearchForm.getPageSize());
        List<FootTax> list = this.findall(footTaxSearchForm.buildSearchCommond());
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footTaxSearchForm 条件
     * @param request           请求
     */
    @Override
    public void deleteAll(FootTaxSearchForm footTaxSearchForm, HttpServletRequest request) {
        if (footTaxSearchForm.getSelectIds() != null) {
            String[] result = footTaxSearchForm.getSelectIds().split(Constants.COMMA);
            for (String id : result) {
                this.delete(Long.parseLong(id), request);
            }
        }
    }

    /**
     * 判断当前日期是否可以抽奖
     *
     * @return 是.否
     */
    @Override
    public boolean getFreeReward() {
        FootTax tax = this.get(11L);
        if (tax == null) {
            return false;
        }
        Date date = new Date();
        int day = DateUtils.getNowDay(date);
        if(day < tax.getFsd().intValue()){
            return true;
        }
        return false;
    }

    /**
     * 获取每日抽奖次数
     *
     * @return
     */
    @Override
    public int getFreeTimes() {
        FootTax tax = this.get(4L);
        if(tax != null){
            return tax.getFsd().intValue();
        }
        return 0;
    }

    /**
     * 获取每次抽取的费用
     *
     * @return
     */
    @Override
    public Double getRewardPay() {
        FootTax tax = this.get(5L);
        if(tax != null){
            return tax.getFsd();
        }
        return 0D;
    }

    /**
     * 获取必获次数
     *
     * @return
     */
    @Override
    public Integer getMustGet() {
        FootTax tax = this.get(8L);
        if(tax != null){
            return tax.getFsd().intValue();
        }
        return 0;
    }

    /**
     * 获取倍率
     *
     * @return
     */
    @Override
    public Integer getOdds() {
        FootTax tax = this.get(6L);
        if(tax != null){
            return tax.getFsd().intValue();
        }
        return 0;
    }

    /**
     * 获取出现高级球员的几率
     *
     * @return
     */
    @Override
    public Integer getShowOdds() {
        FootTax tax = this.get(10L);
        if(tax != null){
            return tax.getFsd().intValue();
        }
        return 0;
    }

    /**
     * 获取停止增长金钱抽奖的次数
     *
     * @return
     */
    @Override
    public Integer getStopIncrease() {
        FootTax tax = this.get(12L);
        if(tax != null){
            return tax.getFsd().intValue();
        }
        return 0;
    }

    /**
     * 获取重置次数
     *
     * @return
     */
    @Override
    public Integer getResetReward() {
        FootTax tax = this.get(7L);
        if (tax != null) {
            return tax.getFsd().intValue();
        }
        return 0;
    }

    /**
     * 获取必获的球员等级主键
     *
     * @return
     */
    @Override
    public Long getDjId() {
        FootTax tax = this.get(13L);
        if (tax != null) {
            return tax.getFsd().longValue();
        }
        return 1L;
    }
}
